package cn.santi.controller;

import cn.santi.domain.*;
import cn.santi.emuns.R;
import cn.santi.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

import static cn.santi.utli.DateTool.gettime;

@RestController
@RequestMapping("/Appointment")
public class AppointmentController {

    @Autowired
    private ACouponService couponService;
    @Autowired
    private ABoxMealService bomMealService;
    @Autowired
    private recordService rs;
    @Autowired
    private SUserService ss;
    @Autowired
    private SAppointmentService appointmentService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/getABoxMealByBid")
    public R getABoxMealByBid(Integer bid) {//根据门店id查询套餐
        LambdaQueryWrapper<ABoxMeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ABoxMeal::getBid, bid).orderByAsc(ABoxMeal::getPrice);
        List<ABoxMeal> list = bomMealService.list(wrapper);
        return R.ok(list);
    }

    @GetMapping("/getCouponByBid")
    public R getCouponByBid(Integer bid) {//根据门店id查询团购券
        LambdaQueryWrapper<ACoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ACoupon::getBid, bid).orderByDesc(ACoupon::getVal);
        List<ACoupon> list = couponService.list(wrapper);
        return R.ok(list);
    }

    @GetMapping("/isUB")
    public void isUB(Integer uid, BigDecimal price) {//判断余额是否足够
//        System.out.println("isUBAuid:" + uid);
//        System.out.println("isUBAprice:" + price);
        //查询用户金额
        SUser one = ss.getById(uid);
//        System.out.println("isUBone:" + one);
        BigDecimal userprice = one.getBalance();
        if (price.compareTo(userprice) >= 0) {
//            return R.fail(300,"余额不足");
            redisTemplate.opsForValue().set(uid + "", "余额不足");// 设置值
        } else {
//            return R.ok("","余额充足");
            redisTemplate.opsForValue().set(uid + "", "余额充足");
        }
    }

    @PostMapping("/addA")
    public R addA(@RequestBody SAppointment a) {//添加订单
        a.setSubmittime(gettime());
//        System.out.println("addAa:" + a);
        // 获取值
        String type = (String) redisTemplate.opsForValue().get(a.getUid()+"");
//        System.out.println("addAtype:" + type);

        String i = "";
        if (type.equals("余额不足")) {//判断是否支付
//            System.out.println("未支付");
            i = "0";
        } else if (type.equals("余额充足")) {
//            System.out.println("已支付未消费");//写修改余额方法
            i = "1";
            //查询用户金额和修改金额
            SUser one = ss.getById(a.getUid());
//            System.out.println("addAone:" + one);
            BigDecimal priceStr = a.getPrice();//new BigDecimal(String.valueOf(a.getPrice()));
            BigDecimal userprice = one.getBalance();
            if (priceStr.compareTo(userprice) >= 0) {
                return R.fail(300, "余额不足");
            }
            BigDecimal pr = userprice.subtract(priceStr);
            one.setBalance(pr);
            ss.updateById(one);
            //创建消费记录
            record record = new record();
            record.setUid(a.getUid());
            record.setDate(gettime());
            record.setType("1");
            record.setPrice("-" + " " + a.getPrice());
            record.setCreatetime("预约消费");
            rs.save(record);
        } else {
            return R.fail("参数错误");
        }
        // 删除键
        redisTemplate.delete(a.getUid()+"");
        a.setAppointmentstatus(i);
        boolean save = appointmentService.save(a);
        if (save) {
            return R.ok("", "添加成功");
        } else {
            return R.fail("添加失败");
        }
    }
}
